#include "TextureManager.h"
#include "ReportError.h"
#include "LoadScreen.h"

TextureManager::TextureManager()
{
	m_it = m_textures.end();
}

bool TextureManager::Load(const std::string& fileName)
{
	File f;
	if(!f.OpenRead(fileName))
	{
		ReportError("Could not open texturepaths\n");
		return false;
	}

	std::string str;
	//std::string tId;
	int tran;
	//bool tranTrue;
	while(!f.EndOf())
	{
		//Format is:
		//filepath
		//transparency bool
		if(!f.GetString(&str))
		{
			ReportError("texture filepath not found\n");
		}


		if(!f.GetInteger(&tran))
		{
			ReportError("transparancy bool not found\n");
		}
		bool b;
		
		if(tran)
		{
			b = true;
		}
		else 
		{
			b = false;
		}

		if(!LoadTex(str, b))
		{
			ReportError("Tex wont load");
		}
		
	}

	return true;
}

Texture* TextureManager::GetTexture(const std::string& path)
{
	return m_textures.find(path)->second;
}

bool TextureManager::LoadTex(const std::string& path, bool tran)
{
	//static int i = 0;
	
		Texture* t = new Texture;
		bool b;
		if(tran == 0)
		{
			b = false;
		}
		else
		{
			b = true;
		}
		
		if(!t->Load(path, b))
		{
			ReportError("texture wont load" + __LINE__);
			assert(0);
		}
		m_textures.insert(std::pair<std::string, Texture*> (path, t));

		static int i = 0;
		i++;
		if(i > 4)
		{
			TheLoadScreen::Instance()->IncrementDraw();
		}
	//}
	return true;

}

void TextureManager::DrawTexture(const std::string& texName)
{
	m_it = m_textures.find(texName);
	assert(m_it != m_textures.end());
	(*m_it).second->Draw();
}